package com.bdqn.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bdqn.dao.BaseDao;
import com.bdqn.dao.ScheduleDao;
import com.bdqn.entity.Schedule;

public class ScheduleDaoImpl extends BaseDao implements ScheduleDao {

	@Override
	public List<Schedule> findAllSchedule() {
		String sql = "select * from Scheduleinfo where 1=1";
		List<Schedule> scList = new ArrayList<Schedule>();
		ResultSet rs = super.executeQuery(sql, null);
		try {
			while (rs.next()) {
				Schedule sc = new Schedule();
				sc.setId(rs.getInt("id"));
				sc.setBoardroomId(rs.getInt("boardroomId"));
				sc.setIntendedName(rs.getString("intendedName"));
				sc.setStartTime(rs.getString("startTime"));
				sc.setDuration(rs.getInt("duration"));
				sc.setEnteringName(rs.getString("enteringName"));
				sc.setReson(rs.getString("reson"));
				scList.add(sc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return scList;
	}

	@Override
	public List<Schedule> findSchedule(String name) {
		StringBuffer sql = new StringBuffer(
				"select * from Scheduleinfo where 1=1");
		List<Schedule> scList = new ArrayList<Schedule>();
		if (name != null && name != " ") {
			String na = "'%" + name + "%'";
			sql.append(" and intendedName like " + na);
		}
		ResultSet rs = super.executeQuery(sql.toString(), null);
		try {
			while (rs.next()) {
				Schedule sc = new Schedule();
				sc.setId(rs.getInt("id"));
				sc.setBoardroomId(rs.getInt("boardroomId"));
				sc.setIntendedName(rs.getString("intendedName"));
				sc.setStartTime(rs.getString("startTime"));
				sc.setDuration(rs.getInt("duration"));
				sc.setEnteringName(rs.getString("enteringName"));
				sc.setReson(rs.getString("reson"));
				scList.add(sc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return scList;
	}

	@Override
	public int addSchedule(Schedule sc) {
		String sql = "INSERT INTO Scheduleinfo(boardroomId,intendedName,startTime,duration,enteringName,reson) VALUE (?,?,?,?,?,?)";
		Object[] obj = { sc.getBoardroomId(), sc.getIntendedName(),
				sc.getStartTime(), sc.getDuration(), sc.getEnteringName(),
				sc.getReson() };
		int result = super.executeUpdate(sql, obj);
		return result;
	}
	@Override
	public int delSchedule(int id) {
		String sql="delete from Scheduleinfo where 1=1";
		if(id!=0){
			sql =sql+ " and id="+id;
		}
		int result=super.executeUpdate(sql, null);
		return result;
	}
}
